﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using BUS;
using Entity;
using DevExpress.XtraGrid.Localization;
using DevExpress.XtraEditors.Controls;
using Share;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;

namespace SLMNG
{
    public partial class CA_Zones : BaseForm
    {
        #region Constructor

        public CA_Zones()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
            StatusOfForm = FormStatus.Add;
            gridView1.ValidateRow += gridView1_ValidateRow;
            gridView1.InvalidRowException += gridView1_InvalidRowException;
            gridView1.ShowingEditor += gridView1_ShowingEditor;
        }

        void gridView1_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView grid = (GridView)sender;
            if (!grid.IsNewItemRow(grid.FocusedRowHandle))
            {
                if (grid.FocusedColumn.Name.Equals("ZoneCode"))
                    e.Cancel = true;
            }
        }

        void gridView1_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = ExceptionMode.NoAction;
        }

        void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            string err = "";
            bool isValid = true;
            if (gridView1.GetRowCellValue(e.RowHandle, "ZoneCode").ToString().Trim() == "")
            {
                err += Messages.ID_IS_NOT_NULL + "\n";
                isValid = false;
            }
            if (gridView1.GetRowCellValue(e.RowHandle, "ZoneName").ToString().Trim() == "")
            {
                err += Messages.NAME_IS_NOT_NULL + "\n";
                isValid = false;
            }

            if (isValid)
            {
                string result = string.Empty;
                if (e.RowHandle < 0)
                {
                    ////Add new
                    result = SaveData(gridView1.GetRowCellValue(e.RowHandle, "ZoneCode").ToString().Trim(),
                                            gridView1.GetRowCellValue(e.RowHandle, "ZoneName").ToString().Trim(),
                                            FormStatus.Add);
                }
                else
                {
                    /// Update
                    result = SaveData(gridView1.GetRowCellValue(e.RowHandle, "ZoneCode").ToString().Trim(),
                                            gridView1.GetRowCellValue(e.RowHandle, "ZoneName").ToString().Trim(),
                                            FormStatus.Edit);
                }

                if (result != "0")
                {
                    e.Valid = false;
                    Global.ShowMessageBox(result, this.Text);
                }
            }
            else
            {
                e.Valid = false;
                Global.ShowMessageBox(err, this.Text);
            }
        }

        #endregion

        #region Form Event

        void comboBoxEdit1_EditValueChanged(object sender, EventArgs e)
        {
            //if (comboBoxEdit1.EditValue.ToString() != string.Empty)
            //{
            //    string strCon = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Documents and Settings\KEN\My Documents\SLM.accdb'";
            //    OleDbConnection oleCon = new OleDbConnection(strCon);
            //    oleCon.Open();
            //    OleDbCommand command = new OleDbCommand("ServiceType_procGetAll", oleCon);
            //    command.CommandType = CommandType.StoredProcedure;
            //    OleDbDataAdapter adapter = new OleDbDataAdapter(command);
            //    DataTable dt = new DataTable();
            //    adapter.Fill(dt);
            //    if (dt != null && dt.Rows.Count > 0)
            //    {
            //        DataTable dt1 = dt.Copy();
            //        dt1.Clear();
            //        string[] arr = comboBoxEdit1.EditValue.ToString().Split(',');
            //        string para = string.Empty;
            //        foreach (string str in arr)
            //        {
            //            para += "'" + str + "',";
            //        }
            //        para = para.Remove(para.Length - 1, 1);
            //        DataRow[] list = dt.Select("ServiceTypeID IN (" + para +")");
            //        foreach (DataRow dr in list)
            //        {
            //            dt1.Rows.Add(dr);
            //        }
            //        gridView.DataSource = dt1;
            //    }
            //}
        }

        void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                GridLocalizer.Active = new MyGridLocalizer();
                LoadDataGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(HandleException.HandleSQLException(ex.Message), this.Text);
            }
        }

        private void LoadDataGrid()
        {
            string[] arg = new string[] { "ZoneCode" };
            object[] obj = new object[] { DBNull.Value };
            DataSet ds1 = CatalogueBase.GetData(Zone.STORE_SELECT, arg, obj);
            gridView.DataSource = ds1.Tables[0];            
        }

        private string SaveData(string code, string name, FormStatus stt)
        {
            string result = "";
            string[] arg = new string[] { "ZoneCode", "ZoneName" };
            object[] obj = new object[] { code, name };
            switch (stt)
            {
                case FormStatus.Add:
                    result = CatalogueBase.SaveData(Zone.STORE_INSERT, arg, obj);
                    break;
                case FormStatus.Edit:
                    result = CatalogueBase.SaveData(Zone.STORE_UPDATE, arg, obj);
                    break;
            }
            return result;
        }

        #endregion

        #region Override Methods

        public override void fncInsert()
        {
            tabDanhMuc111.SelectedTabPageIndex = 1;
            ActionOnControl(new Control[] { txtZoneCode }, true);
            StatusOfForm = FormStatus.Add;
            ClearForm();
        }        

        public override void fncSave()
        {
            if (tabDanhMuc111.SelectedTabPageIndex == 1)
            {
                if (string.IsNullOrEmpty(txtZoneCode.Text))
                {
                    MessageBox.Show(temp.ID_IS_NOT_NULL);
                    return;
                }
                else if (string.IsNullOrEmpty(txtZoneName.Text))
                {
                    MessageBox.Show(temp.NAME_IS_NOT_NULL);
                    return;
                }
                
                string result = string.Empty;
                if(StatusOfForm == FormStatus.Add)
                    result = SaveData(txtZoneCode.Text.Trim(),txtZoneName.Text.Trim(), FormStatus.Add);
                else if(StatusOfForm == FormStatus.Edit)
                    result = SaveData(txtZoneCode.Text.Trim(), txtZoneName.Text.Trim(), FormStatus.Edit);

                if (result == "0")
                {
                    if (StatusOfForm == FormStatus.Add)
                    {
                        ClearForm();
                        MessageBox.Show(temp.STATUS_INSERT_SUCCESS,this.Text);
                    }
                    else
                        MessageBox.Show(temp.STATUS_UPDATE_SUCCESS, this.Text);
                }
                else
                    MessageBox.Show(HandleException.HandleSQLException(result),this.Text);

            }
        }

        private void ClearForm()
        {
            txtZoneCode.Text = string.Empty;
            txtZoneName.Text = string.Empty;
            txtZoneCode.Focus();
        }

        public override void fncEdit()
        {
            if (gridView1.FocusedRowHandle >= 0)
            {
                tabDanhMuc111.SelectedTabPageIndex = 1;
                ActionOnControl(new Control[] { txtZoneCode }, false);
                StatusOfForm = FormStatus.Edit;

                string[] arg = new string[] { "ZoneCode" };
                object[] obj = new object[] { gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ZoneCode").ToString() };
                DataTable dt = CatalogueBase.GetData(Zone.STORE_SELECT, arg, obj).Tables[0];
                txtZoneCode.Text = dt.Rows[0]["ZoneCode"].ToString();
                txtZoneName.Text = dt.Rows[0]["ZoneName"].ToString();
            }
        }

        public override void fncDelete()
        {
            if (gridView1.FocusedRowHandle >= 0)
            {
                MessageBox.Show(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ZoneCode").ToString());
            }
        }

        public override void fncRefresh()
        {
            LoadDataGrid();
        }

        #endregion
    }
}
